package com.power.using.mytoutiao.myapp;

import android.content.Context;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.multidex.MultiDexApplication;

import com.power.using.mytoutiao.BuildConfig;
import com.power.using.mytoutiao.manager.SdkManager;
import com.power.using.mytoutiao.util.SettingUtil;

import java.util.Calendar;

/**
 * Created by 聪明一只哈 on 2020/4/2.
 * Time: 2020/4/2  10:33
 */
public class InitApp extends MultiDexApplication {

    public static Context AppContext;

    @Override
    public void onCreate() {
        super.onCreate();
        AppContext=getApplicationContext();

        initTheme();

        if(BuildConfig.DEBUG){
            SdkManager.initStetho(this);
            SdkManager.initLeakCanary(this);
        }
    }

    private void initTheme() {

        SettingUtil settingUtil = SettingUtil.getInstance();
        if(settingUtil.getIsAutoNightMode()){

            int nightStartHour = Integer.parseInt(settingUtil.getNightStartHour());
            int nightStartMinute = Integer.parseInt(settingUtil.getNightStartMinute());
            int dayStartHour = Integer.parseInt(settingUtil.getDayStartHour());
            int dayStartMinute = Integer.parseInt(settingUtil.getDayStartMinute());

            Calendar calendar = Calendar.getInstance();
            int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
            int currentMinute = calendar.get(Calendar.MINUTE);

            int nightValue = nightStartHour * 60 + nightStartMinute;
            int dayValue = dayStartHour * 60 + dayStartMinute;
            int currentValue = currentHour * 60 + currentMinute;

            if (currentValue >= nightValue || currentValue <= dayValue) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                settingUtil.setIsNightMode(true);
            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                settingUtil.setIsNightMode(false);
            }

        }else{

            // 获取当前主题
            if (settingUtil.getIsNightMode()) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
        }


    }
}
